<?php

namespace paymch\modules\test\controllers;

use Yii;
use yii\web\Controller;
use common\jobs\DownloadJob;
use common\jobs\TradeNotifyJob;

/**
 * QueueController 
 */
class QueueController extends Controller {

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex() {
        $log = new \common\models\logger\Log();
        $log->attributes = [
            'level' => 0,
            'category' => 'TradeNotifyJob',
            'log_time' => time(),
            'prefix' => '20',
            'message' => '20秒执行',
        ];

        $data = [];
        if ($log->save()) {

            $id = Yii::$app->queue->delay(10)->push(new TradeNotifyJob([
                'logid' => $log->id
            ]));

            Yii::$app->queue->remove($id);

            $isWaiting = Yii::$app->queue->isWaiting($id);

            // Worker gets the job from queue, and executing it.
            $isReserved = Yii::$app->queue->isReserved($id);

            // Worker has executed the job.
            $isDone = Yii::$app->queue->isDone($id);
            $data = [
                'id' => $id,
                'isWaiting' => $isWaiting,
                'isReserved' => $isReserved,
                'isDone' => $isDone,
            ];
        } else {
            $data['error'] = $log->getErrors();
        }


//        $id = Yii::$app->queue->push(new DownloadJob([
//            'url' => 'http://mccdn.qcloud.com/static/img/c9a9de3e9a0c35c46c82c85fa3a86de8/150x150+CVM.jpg',
//            'file' => '@runtime/tmp/image.jpg',
//        ]));
//
//        $id = Yii::$app->queue->delay(5 * 60)->push(new DownloadJob([
//            'url' => 'http://mccdn.qcloud.com/static/img/c9a9de3e9a0c35c46c82c85fa3a86de8/150x150+CVM.jpg',
//            'file' => '@runtime/tmp/image1.jpg',
//        ]));
        // The job is waiting for execute.


        return \yii\helpers\VarDumper::export($data);
    }

}
